package cc.vv.baselibrary.util;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import cc.vv.baselibrary.R;
import cc.vv.baselibrary.global.BCSharePreKey;

/**
 * 设置背景色
 * Created by Chen.qi on 2018/8/1.
 */
public class BCColorSetBackgroundUtil {

    /**
     * 设置主色
     *
     * @param view 控件
     */
    public static void setBackgroundMainColor(View view) {
        if (null != view) {
            int mainColor = LKPrefUtil.getInt(BCSharePreKey.THEME_MAIN_COLOR, 0);
            if (0 != mainColor) {
                view.setBackgroundColor(mainColor);
            }
        }
    }

    /**
     * 设置控件字体主色
     *
     * @param textView 控件
     */
    public static void setTextColorMainColor(TextView textView) {
        if (null != textView) {
            int mainColor = LKPrefUtil.getInt(BCSharePreKey.THEME_MAIN_COLOR, 0);
            if (0 != mainColor) {
                textView.setTextColor(mainColor);
            }
        }
    }

    /**
     * 设置辅色
     *
     * @param view 控件
     */
    public static void setBackgroundComplementaryColor(View view) {
        if (null != view) {
            int complementaryColor = LKPrefUtil.getInt(BCSharePreKey.THEME_COMPLEMNTARY_COLOR, 0);
            if (0 != complementaryColor) {
                view.setBackgroundColor(complementaryColor);
            }
        }
    }

    /**
     * 设置其他色
     *
     * @param view 控件
     */
    public static void setBackgroundOtherColor(View view) {
        if (null != view) {
            int otherColor = LKPrefUtil.getInt(BCSharePreKey.THEME_OTHER_COLOR, 0);
            if (0 != otherColor) {
                view.setBackgroundColor(otherColor);
            }
        }
    }

    /**
     * 设置字体选中、未选中的状态颜色
     *
     * @param textView      textView控件
     * @param unSelectColor 未选中的颜色，默认颜色为服务器换肤的主色
     */
    public static void setTextColorStatus(TextView textView, int unSelectColor) {
        try {
            int selectedColor = LKPrefUtil.getInt(BCSharePreKey.THEME_MAIN_COLOR, 0);
            if (null != textView && selectedColor != 0) {
                int[] colors = new int[]{selectedColor, unSelectColor};
                int[][] states = new int[2][];
                states[0] = new int[]{android.R.attr.state_selected, selectedColor};
                states[1] = new int[]{-android.R.attr.state_selected, unSelectColor};
                ColorStateList colorStateList = new ColorStateList(states, colors);
                textView.setTextColor(colorStateList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置选中的颜色和圆角
     *
     * @param radius
     * @return
     */
    private static Drawable getDrawable_select(float radius) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setCornerRadius(radius);//设置圆角半径
        int selectedColor = LKPrefUtil.getInt(BCSharePreKey.THEME_MAIN_COLOR, 0);
        gradientDrawable.setColor(selectedColor);
        return gradientDrawable;
    }

    /**
     * 设置未选中的颜色和圆角
     *
     * @param radius
     * @return
     */
    private static Drawable getDrawable_normal(int idNorm, float radius) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setCornerRadius(radius);//设置圆角半径
        gradientDrawable.setColor(idNorm);
        return gradientDrawable;
    }

    /**
     * 换肤圆角
     *
     * @param idNormal 未选中的颜色
     * @param radius   圆角大小
     * @return
     */
    public static StateListDrawable addStateListBgDrawableRadius1(int idNormal, int radius) {
        StateListDrawable drawable = new StateListDrawable();
        if (idNormal != 0) {
            drawable.addState(new int[]{android.R.attr.state_selected}, getDrawable_select(radius));
            drawable.addState(new int[]{-android.R.attr.state_selected}, getDrawable_normal(idNormal, radius));
            drawable.addState(new int[]{android.R.attr.state_pressed}, getDrawable_select(radius));
            drawable.addState(new int[]{-android.R.attr.state_enabled}, getDrawable_normal(idNormal, radius));
        }
        return drawable;
    }

    /**
     * 换肤圆角
     *
     * @param idNormal 未选中的颜色
     * @param radius   圆角大小
     * @return
     */
    public static StateListDrawable addStateListBgDrawableRadius(int idNormal, int radius) {
        StateListDrawable drawable = new StateListDrawable();
        int selectedColor = LKPrefUtil.getInt(BCSharePreKey.THEME_MAIN_COLOR, 0);
        if (selectedColor != 0 && idNormal != 0) {
            drawable.addState(new int[]{android.R.attr.state_selected}, getDrawable_select(radius));
            drawable.addState(new int[]{-android.R.attr.state_selected}, getDrawable_normal(idNormal, radius));
            drawable.addState(new int[]{android.R.attr.state_pressed}, getDrawable_select(radius));
            drawable.addState(new int[]{-android.R.attr.state_enabled}, getDrawable_normal(idNormal, radius));
        }
        return drawable;
    }


    /**
     * 针对边框是线条（新增收货地址标签样式）
     */
    public static StateListDrawable addStateLine(int radius, int normalColor) {
        StateListDrawable drawable = new StateListDrawable();
        int selectedColor = LKPrefUtil.getInt(BCSharePreKey.THEME_MAIN_COLOR, 0);
        if (selectedColor != 0) {
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setCornerRadius(radius);//设置圆角半径
            gradientDrawable.setStroke(1, normalColor);
            gradientDrawable.setColor(selectedColor);


            GradientDrawable gradientDrawableNormal = new GradientDrawable();
            gradientDrawableNormal.setCornerRadius(radius);//设置圆角半径
            gradientDrawable.setStroke(1, normalColor);
            gradientDrawable.setColor(normalColor);

            drawable.addState(new int[]{android.R.attr.state_selected}, gradientDrawable);
            drawable.addState(new int[]{-android.R.attr.state_selected}, gradientDrawableNormal);
            drawable.addState(new int[]{android.R.attr.state_pressed}, gradientDrawable);
            drawable.addState(new int[]{-android.R.attr.state_enabled}, gradientDrawableNormal);
        }
        return drawable;
    }

}